import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:share/share.dart';

class TextPage extends StatefulWidget {
  List<String> list;
  TextPage(this.list);
  @override
  _TextPageState createState() => _TextPageState();
}

class _TextPageState extends State<TextPage> {
  List<String> resultList = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    resultList = widget.list;
    // for(String i in resultList){
    //   if(i==""){
    //     resultList.remove(i);
    //   }
    // }
    setState(() {

    });
  }
  share()async{
    String re = resultList.toString().substring(1,resultList.toString().length-1);
    Share.share(re);
  }
  copyall()async{
    String re = resultList.toString().substring(1,resultList.toString().length-1);
    Clipboard.setData(ClipboardData(text: re));
    Fluttertoast.showToast(msg: "Copy success!");
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        title: Text("Result",style: TextStyle(color: Colors.black),),
        centerTitle: true,
        elevation: 0,
        backgroundColor: Colors.transparent,
        actions:[
          TextButton(onPressed: (){
            Navigator.pop(context);
            // Navigator.pop(context,widget.resultFile);
          }, child: Text("Done"))
        ] ,
      ),
      body: Column(
        children: [
          Expanded(
            child: Container(
              // height: double.infinity,
              // width: double.infinity,
              decoration: BoxDecoration(
                  color: Color(0xfffdfded),
                  borderRadius: BorderRadius.circular(10)
              ),
              padding: EdgeInsets.symmetric(horizontal: 16),
              margin: EdgeInsets.only(left: 16,right: 16),
              child: ListView.builder(itemBuilder: (context,index)=>ListTile(
                contentPadding: EdgeInsets.all(0),
                trailing: Icon(Icons.copy_rounded,color: Color(0xffdddddd),size: 15,),
                onTap: (){
                  Clipboard.setData(ClipboardData(text: resultList[index]));
                  Fluttertoast.showToast(msg: "Copy Success");
                },
                leading: GestureDetector(
                  onTap: (){
                    resultList.removeAt(index);
                    setState(() {
                    });
                  },
                  child: Icon(Icons.close_rounded,size: 25,),
                ),
                title: Text(resultList[index],style: TextStyle(fontWeight: FontWeight.w500),),
              ),
                physics: BouncingScrollPhysics(),
                itemCount: resultList.length,
              ),
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              GestureDetector(
                onTap: copyall,
                child: Container(
                  height: 50,
                  // width: 50,
                  margin: EdgeInsets.symmetric(vertical: 30),
                  padding: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(50),
                      color: Color(0xff4474fb)
                  ),
                  child: Row(
                    children: [
                      Icon(Icons.file_copy,color: Colors.white,),
                      SizedBox(width: 10,),
                      Text("copy all",style: TextStyle(fontWeight: FontWeight.w500,color: Colors.white),),
                    ],
                  )
                ),
              ),
              SizedBox(width: 10,),
              GestureDetector(
                onTap: share,
                child: Container(
                  height: 50,
                  width: 50,
                  margin: EdgeInsets.symmetric(vertical: 30),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(50),
                      color: Color(0xffadadfd)
                  ),
                  child: Icon(Icons.ios_share,color: Colors.white,),
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
